package com.example.hxd.zndzg.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;


import com.example.hxd.zndzg.R;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;


public class SettingFragmeng extends Fragment{
    private View view;
    private QMUIGroupListView mGroupListView;
    private List<QMUICommonListItemView> listItemViews;
    private String[] funName = new String[]{"功能模块一", "功能模块二", "功能模块三", "功能模块四"};
    private Context mContext;
    private Activity mActivity;

    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    @SuppressLint("ValidFragment")
    public SettingFragmeng(Context mContext, Activity mActivity) {
        this.mContext = mContext;
        this.mActivity = mActivity;
    }

    public SettingFragmeng() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_setting, null);
        initView();
        return view;
    }

    private void initView() {
        mGroupListView = view.findViewById(R.id.groupListView);
        initItemView();
    }

    private void initItemView(){
        listItemViews = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            QMUICommonListItemView itemWithChevron = mGroupListView.createItemView(funName[i]);
            itemWithChevron.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
            listItemViews.add(itemWithChevron);
        }

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof QMUICommonListItemView) {
                    int flag = 0;
                    CharSequence text = ((QMUICommonListItemView) v).getText();
                    for (int i = 0; i < funName.length; i++) {
                        if (text.equals(funName[i])){
                            flag = i;
                            break;
                        }
                    }
                    switch (flag){
                        case 0:
                            funModificationOfOne();
                            break;

                        case 1:
                            funModificationOfTwo();
                            break;

                        case 2:
                            funModificationOfThree();
                            break;

                        case 3:
                            funModificationOfFour();
                            break;

                    }
                    Toast.makeText(mContext, text + " is Clicked", Toast.LENGTH_SHORT).show();
                }
            }
        };

        QMUIGroupListView.newSection(mContext)
                .setTitle("功能设置 ")
                .addItemView(listItemViews.get(0), onClickListener)
                .addItemView(listItemViews.get(1), onClickListener)
                .addItemView(listItemViews.get(2), onClickListener)
                .addItemView(listItemViews.get(3), onClickListener)
                .addTo(mGroupListView);
    }

    /**
     * 功能模块一
     */
    private void funModificationOfOne(){
        final String[] items = new String[]{"选项1", "选项2", "选项3"};
        final int checkedIndex = 1;
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setCheckedIndex(checkedIndex)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "你选择了 " + items[which], Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    /**
     * 功能模块二
     */
    private void funModificationOfTwo(){
        new QMUIDialog.CheckBoxMessageDialogBuilder(mContext)
                .setTitle("是否初始化智能电子柜信息?")
                .setMessage("初始化信息")
                .setChecked(true)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    /**
     * 功能模块三
     */
    private void funModificationOfThree(){
        final String[] items = new String[]{"选项1", "选项2", "选项3", "选项4", "选项5", "选项6"};
        final QMUIDialog.MultiCheckableDialogBuilder builder = new QMUIDialog.MultiCheckableDialogBuilder(mContext)
                .setCheckedItems(new int[]{1, 3})
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                String result = "你选择了 ";
                for (int i = 0; i < builder.getCheckedItemIndexes().length; i++) {
                    result += "" + builder.getCheckedItemIndexes()[i] + "; ";
                }
                Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        builder.create(mCurrentDialogStyle).show();
    }

    /**
     * 功能模块四
     */
    private void funModificationOfFour(){
        final String[] items = new String[]{"选项1", "选项2", "选项3"};
        final int checkedIndex = 1;
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setCheckedIndex(checkedIndex)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "你选择了 " + items[which], Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }
}
